﻿using GalaSoft.MvvmLight;
using System;
using Newtonsoft.Json;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace Kinlo.UI.Main.Hepler
{
    public class CounterNing : ViewModelBase
    {

        int inCount;
        /// <summary>
        /// 投入
        /// </summary>
        public int INCount
        {
            get { return inCount; }
            set
            {
                inCount = value;
                RaisePropertyChanged(() => INCount);
                RaisePropertyChanged(() => QualifiedCountRate);
                Save();
            }
        }

        int outCount;
        /// <summary>
        /// 产出
        /// </summary>
        public int OUTCount
        {
            get { return outCount; }
            set
            {
                outCount = value;
                RaisePropertyChanged(() => OUTCount);
                RaisePropertyChanged(() => QualifiedCountRate);
                Save();
            }
        }

        int okCount;
        /// <summary>
        /// OK数量
        /// </summary>
        public int OKCount
        {
            get { return okCount; }
            set
            {
                okCount = value;
                RaisePropertyChanged(() => OKCount);
                RaisePropertyChanged(() => QualifiedCountRate);
                Save();
            }
        }

        int ngCount;
        /// <summary>
        /// NG数量
        /// </summary>
        public int NGCount
        {
            get { return ngCount; }
            set
            {
                ngCount = value;
                RaisePropertyChanged(() => NGCount);
                RaisePropertyChanged(() => QualifiedCountRate);
                Save();
            }
        }

        int leakHuntingNgCount;
        /// <summary>
        /// 测漏NG数量
        /// </summary>
        public int LeakHuntingNgCount
        {
            get { return leakHuntingNgCount; }
            set
            {
                leakHuntingNgCount = value;
                RaisePropertyChanged(() => LeakHuntingNgCount);
                Save();
            }
        }

        int weightNgCount;
        /// <summary>
        /// 称重NG数量
        /// </summary>
        public int WeightNgCount
        {
            get { return weightNgCount; }
            set
            {
                weightNgCount = value;
                RaisePropertyChanged(() => WeightNgCount);
                Save();
            }
        }

        int notInjCount;
        /// <summary>
        /// 未注液
        /// </summary>
        public int NotInjCount
        {
            get { return notInjCount; }
            set
            {
                notInjCount = value;
                RaisePropertyChanged(() => NotInjCount);
                RaisePropertyChanged(() => QualifiedCountRate);
                Save();
            }
        }

        public string QualifiedCountRate
        {
            get { return (OKCount / (OKCount + NGCount + 0.0001)).ToString("P2"); }
        }
        object locker = new object();
        public void ClearZero()
        {
            lock (locker)
            {
                INCount = 0;
                OUTCount = 0;
                OKCount = 0;
                NGCount = 0;
                WeightNgCount = 0;
                LeakHuntingNgCount = 0;
                NotInjCount = 0;
            }
        }

        static string counterPath = "Counter.json";
        public bool Save()
        {
            try
            {
                var str = JsonConvert.SerializeObject(this, Newtonsoft.Json.Formatting.Indented);
                File.WriteAllText(counterPath, str);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public static CounterNing Load()
        {
            try
            {
                var str = File.ReadAllText(counterPath);
                var counter = JsonConvert.DeserializeObject<CounterNing>(str);
                if (counter != null)
                    return counter;
                return new CounterNing();
            }
            catch (Exception)
            {
                return new CounterNing();
            }
        }

    }
}
